package com.braveman.im.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.braveman.im.R;
import com.braveman.im.bean.PicUpdateBean;
import com.braveman.im.bean.SendMomentBean;
import com.braveman.im.manager.ApiManager;
import com.braveman.im.ui.adapter.PhotoAdapter;
import com.braveman.im.ui.weiget.RecyclerItemClickListener;
import com.braveman.im.utils.UtilsStyle;
import com.ht.baselibs.core.EventCenter;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.ui.widget.TitleBarView;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.LogUtils;
import com.ht.baselibs.utils.UIUtils;
import com.vondear.rxtools.RxBarTool;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import me.iwf.photopicker.PhotoPicker;
import me.iwf.photopicker.PhotoPreview;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Response;

public class SendMomentActivity extends BaseActivity {

    private PhotoAdapter photoAdapter;
    private ArrayList<String> selectedPhotos = new ArrayList<>();
    private RecyclerView recyclerView;
    private File file;
    private static final int REQUSET_CODE_SEND_MOMENT = 200;
    private static final int REQUSET_CODE_SEND_MOMENT_CAN_LOOK = 201;
    private List<String> appLicationList = new ArrayList<>();
    private EditText mEtActivitySendLoveAddressDetails;
    private ArrayList<String> friendsId = new ArrayList<>();//提醒好友id列表
    private ArrayList<String> canLookFriendsId = new ArrayList<>();//给谁看好友Id列表
    private ArrayList<String> noLookfriendsId = new ArrayList<>();//不给谁看好友Id列表
    private String canLook = "all";
    private EditText mEtActivitySendMomentDetails;


    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("发布诚友圈").setLeftBtnIcon(R.mipmap.back_black).setOnLeftBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityUtils.finishActivity(SendMomentActivity.this);
            }
        });
        mTitleBarView.addActions(new TitleBarView.Action(R.color.color_19, "发布") {
            @Override
            public void performAction(View view) {
                doNetWork();
            }
        });
    }

    private void doNetWork() {
        SendMomentBean sendMomentBean = new SendMomentBean();
        sendMomentBean.photoList.addAll(appLicationList);
        String content = mEtActivitySendMomentDetails.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            UIUtils.showToast("请输入要发布的内容");
            return;
        }
        if (friendsId != null) {
            sendMomentBean.mentionIdList.addAll(friendsId);
        }

        sendMomentBean.content = content;
        switch (canLook) {
            case "all":
                sendMomentBean.shareTo = "all";
                break;
            case "justMe":
                sendMomentBean.shareTo = "justMe";
                break;
            case "justSelected":
                sendMomentBean.shareTo = "justSelected";
                sendMomentBean.selectedIdList.addAll(canLookFriendsId);
                break;
            case "excludeSelected":
                sendMomentBean.shareTo = "justSelected";
                if (canLookFriendsId != null) {
                    sendMomentBean.selectedIdList.addAll(noLookfriendsId);
                }
                break;
        }


        ApiManager.getApi().sendMoment(sendMomentBean).enqueue(new NetCallBack<SendMomentBean>() {
            @Override
            protected void onSuccess(Call<SendMomentBean> call, Response<SendMomentBean> response) {
                if (response != null) {
                    UIUtils.showToast("发送朋友圈成功");
                    EventBus.getDefault().post(new EventCenter(EventCode.EVENT_UPDATA_DELETE_MOMENT));
                    ActivityUtils.finishActivity(SendMomentActivity.this);
                }
            }
        });
    }

    @Override
    protected int initContentView() {
        UtilsStyle.setStatusBarMode(this, true);
        RxBarTool.FlymeSetStatusBarLightMode(getWindow(), true);
        RxBarTool.MIUISetStatusBarLightMode(getWindow(), true);
        return R.layout.activity_send_moment;
    }

    @Override
    protected void initChildView() {
        recyclerView = findViewById(R.id.recycler_view);
        mEtActivitySendLoveAddressDetails = (EditText) findViewById(R.id.et_activity_send_love_address_details);
        mEtActivitySendMomentDetails = (EditText) findViewById(R.id.et_activity_send_moment_details);

    }

    @Override
    protected void initChildData() {
        photoAdapter = new PhotoAdapter(this, selectedPhotos);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, OrientationHelper.VERTICAL));
        recyclerView.setAdapter(photoAdapter);
    }

    @Override
    protected void setChildViewListener() {

        recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this,
                new RecyclerItemClickListener.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        if (photoAdapter.getItemViewType(position) == PhotoAdapter.TYPE_ADD) {
                            PhotoPicker.builder()
                                    .setPhotoCount(PhotoAdapter.MAX)
                                    .setShowCamera(true)
                                    .setPreviewEnabled(false)
                                    .setSelected(selectedPhotos)
                                    .start(SendMomentActivity.this);
                        } else {
                            PhotoPreview.builder()
                                    .setPhotos(selectedPhotos)
                                    .setCurrentItem(position)
                                    .start(SendMomentActivity.this);
                        }
                    }
                }));
        findViewById(R.id.bti_activity_send_moment_remind).setOnClickListener(this);
        findViewById(R.id.bti_activity_send_moment_can_look).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.bti_activity_send_moment_remind:
                Bundle bundle = new Bundle();
                bundle.putInt("listType", 3);
                ActivityUtils.startActivityForResult(SendMomentActivity.this, SelectFriendsActivity.class, REQUSET_CODE_SEND_MOMENT);//提醒谁看
                break;
            case R.id.bti_activity_send_moment_can_look:
                ActivityUtils.startActivityForResult(SendMomentActivity.this, GiveWhosCanLookActivity.class, REQUSET_CODE_SEND_MOMENT_CAN_LOOK);//给谁看
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PhotoPicker.REQUEST_CODE:

                    List<String> photos = null;
                    selectedPhotos.clear();
                    if (data != null) {
                        photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
                        MultipartBody.Part[] part = new MultipartBody.Part[photos.size()];

                        if (photos != null && photos.size() > 0) {

                            for (int x = 0; x < photos.size(); x++) {

                                String path = photos.get(x);

                                file = new File(path);
                                RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);

                                part[x] = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
                            }
                            showLoadingDialog();
                            ApiManager.getApi().uploadPicGroup("image", part)
                                    .enqueue(new NetCallBack<List<PicUpdateBean>>() {
                                        @Override
                                        protected void onSuccess(Call<List<PicUpdateBean>> call, Response<List<PicUpdateBean>> response) {
                                            LogUtils.debug(response.body().toString());
                                            if (response != null) {
                                                UIUtils.showToast("图片上传成功");
                                                List<PicUpdateBean> picUpdateBeanList = response.body();
                                                if (picUpdateBeanList != null && picUpdateBeanList.size() > 0) {
                                                    if (appLicationList.size() != 0) {
                                                        appLicationList.clear();
                                                    }
                                                    for (PicUpdateBean piv : picUpdateBeanList) {
                                                        appLicationList.add(piv.url);
                                                    }

                                                }
                                            }
                                        }

                                        @Override
                                        protected void hiddenProgress() {
                                            hideLodingDialog();
                                        }
                                    });
                        }

                    }
                    if (photos != null) {

                        selectedPhotos.addAll(photos);

                    }
                    photoAdapter.notifyDataSetChanged();
                    break;
                case PhotoPreview.REQUEST_CODE:
                    List<String> photos1 = null;
                    if (data != null) {
                        photos1 = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
                    }
                    selectedPhotos.clear();

                    if (photos1 != null) {

                        selectedPhotos.addAll(photos1);
                    }
                    photoAdapter.notifyDataSetChanged();
                    break;
                case REQUSET_CODE_SEND_MOMENT:
                    if (data != null)
                        friendsId = data.getStringArrayListExtra("friendsId");
                    break;
                case REQUSET_CODE_SEND_MOMENT_CAN_LOOK:
                    if (data != null) {
                        canLook = data.getStringExtra("canLook");
                        switch (canLook) {
                            case "all":
                                break;
                            case "justMe":
                                break;
                            case "justSelected":
                                canLookFriendsId = data.getStringArrayListExtra("canLookList");
                                break;
                            case "excludeSelected":
                                noLookfriendsId = data.getStringArrayListExtra("canLookList");
                                break;
                        }
                    }
                    break;
            }
        }

    }


}

